package org.linlinjava.litemall.admin.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.admin.annotation.LoginAdmin;
import org.linlinjava.litemall.db.domain.LitemallOrder;
import org.linlinjava.litemall.db.domain.LitemallOrderGoods;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.service.LitemallOrderGoodsService;
import org.linlinjava.litemall.db.service.LitemallOrderService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.linlinjava.litemall.db.util.OrderUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/order")
public class OrderController {
    private final Log logger = LogFactory.getLog(OrderController.class);

    @Autowired
    private LitemallOrderService orderService;
    @Autowired
    private LitemallOrderGoodsService orderGoodsService;
    @Autowired
    private LitemallUserService userService;


    @GetMapping("/list")
    public Object list(@LoginAdmin Integer adminId,
                       Integer userId, String orderSn,
                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "limit", defaultValue = "10") Integer limit,
                       String sort, String orderBy, Short orderStatus, String mobile, String consignee) {
        if (adminId == null) {
            return ResponseUtil.fail401();
        }
        LitemallOrder order = new LitemallOrder();
        order.setUserId(userId);
        order.setOrderSn(orderSn);
        order.setOrderStatus(orderStatus);
        order.setMobile(mobile);
        order.setConsignee(consignee);
        List<LitemallOrder> orderList = orderService.querySelective(page, limit, order);
        for (LitemallOrder o : orderList) {
            o.setOrderStatusText(OrderUtil.orderStatusText(o));
        }
        int total = orderService.countSelective(page, limit, order);

        Map<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", orderList);

        return ResponseUtil.ok(data);
    }

    /*
     * 目前的逻辑不支持管理员创建
     */
    @PostMapping("/create")
    public Object create(@LoginAdmin Integer adminId, @RequestBody LitemallOrder order) {
        if (adminId == null) {
            return ResponseUtil.unlogin();
        }
        return ResponseUtil.unsupport();
    }

    @GetMapping("/read")
    public Object read(@LoginAdmin Integer adminId, Integer id) {
        if (adminId == null) {
            return ResponseUtil.fail401();
        }

        LitemallOrder order = orderService.findById(id);
        return ResponseUtil.ok(order);
    }


    @PostMapping("/update")
    public Object update(@LoginAdmin Integer adminId, @RequestBody LitemallOrder order) {
        if (adminId == null) {
            return ResponseUtil.unlogin();
        }

        Integer orderId = order.getId();
        if (orderId == null) {
            return ResponseUtil.badArgument();
        }

        LitemallOrder litemallOrder = orderService.findById(orderId);
        if (litemallOrder == null) {
            return ResponseUtil.badArgumentValue();
        }

        if (OrderUtil.isPayStatus(litemallOrder) || OrderUtil.isShipStatus(litemallOrder)) {
            LitemallOrder newOrder = new LitemallOrder();
            newOrder.setId(orderId);
            newOrder.setUserId(order.getUserId());
            newOrder.setShipChannel(order.getShipChannel());
            newOrder.setShipSn(order.getShipSn());
            newOrder.setShipStartTime(order.getShipStartTime());
            newOrder.setShipEndTime(order.getShipEndTime());
            newOrder.setOrderStatus(order.getOrderStatus());
            orderService.updateRev(newOrder);
        } else {
            return ResponseUtil.badArgumentValue();
        }

        litemallOrder = orderService.findById(orderId);
        litemallOrder.setOrderStatusText(OrderUtil.orderStatusText(litemallOrder));
        return ResponseUtil.ok(litemallOrder);
    }

    /*
     * 目前仅仅支持管理员设置发货相关的信息
     */
    @PostMapping("/updateDetail")
    public Object updateDetail(@LoginAdmin Integer adminId, @RequestBody LitemallOrder order) {
        if (adminId == null) {
            return ResponseUtil.unlogin();
        }

        Integer orderId = order.getId();
        if (orderId == null) {
            return ResponseUtil.badArgument();
        }

        orderService.update(order);


        LitemallOrder litemallOrder = orderService.findById(orderId);
        litemallOrder.setOrderStatusText(OrderUtil.orderStatusText(litemallOrder));

        return ResponseUtil.ok(litemallOrder);
    }

    @PostMapping("/delete")
    public Object delete(@LoginAdmin Integer adminId, @RequestBody LitemallOrder order) {
        if (adminId == null) {
            return ResponseUtil.unlogin();
        }
        return ResponseUtil.unsupport();
    }

    /**
     * 查询订单详情
     *
     * @param adminId
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Object detail(@LoginAdmin Integer adminId, Integer id) {
        if (adminId == null) {
            return ResponseUtil.unlogin();
        }
        List<LitemallOrderGoods> goods = orderGoodsService.queryByOid(id);
        Map<String, Object> map = new HashMap<>();
        map.put("goods", goods);
        return ResponseUtil.ok(map);
    }

}
